//打印采购单
app.controller('purchasePrintController', ['$scope','ajax','$stateParams','$state',function ($scope,ajax,$stateParams,$state) {

    function sliceFour(arr){
        var newArr = [];
        for(var i = 0,length= arr.length; i<length; i++){
            var q = Math.floor(i/4);
            if(!newArr[q]){
                newArr[q]=[];
            }
            newArr[q].push(arr[i]);
        }
        return newArr;
    }

    $stateParams.Soring==1 ? showPrintStoring(): showprint();

    function showprint(){
        $scope.showPrintSorting=true;
        ajax.get({
            url: 'back/purchaseOrder/printPurchaseOrder',
            data:{
                purchaseOrderId:$stateParams.id,
                pageSize: 999
            },
            debug: true
        }).then(function (result) {
            var a = result;
            a.meat.length = 30;
            a.seasoning.length = 60;
            a.vegetables.length = 90;
            a.vegetables1  = a.vegetables.slice(0,30);
            a.vegetables2  = a.vegetables.slice(30,60);
            a.vegetables3  = a.vegetables.slice(60,90);
            a.seasoning1  = a.seasoning.slice(0,30);
            a.seasoning2  = a.seasoning.slice(30,60);
            a.rice.length = 30;
            a.aquatic.length = 30;
            $scope.list = a;
        })
    }

    function showPrintStoring(){
        $scope.showPrintSorting=false;
        ajax.get({
            url: 'back/purchaseOrder/printSorting',
            data:{
                purchaseOrderId:$stateParams.id,
            },
            debug: true
        }).then(function (result) {
            var len=result.length;
            for(var i=0;i<len;i++){
                result[i].orders.length=28
            }
            $scope.listSorting = sliceFour(result);
            console.log($scope.listSorting)
        })
    }

    $scope.blur = function(orderDetailId,trueNumber){
        if(orderDetailId == undefined){
            return false
        }
        else {
            ajax.post({
                url: "back/order/updateOrderDetailTrueNumber",
                data: {
                    orderDetailId: orderDetailId,
                    trueNumber: trueNumber
                }
            }).then(function (result) {
                Flash.create('success','修改实际数量成功', 'custom-class');

                $state.go('admin.linePurchaseOrder',null,{
                    reload:true
                });

            });
        }

    }
}]);